#################################### Appendix (D) ##############################################
################################################################################################

# Load the required packages
rm(list = ls())
library(fixest)
library(readstata13)


#Loading the main dataset: 
# data_2rounds: Contains data on the 2 rounds of the election, excluding the pre-electoral period 

data <- read_dta("data_2rounds.dta")
data3 <- read_dta("data_3rounds.dta")

#Create dictionary to present the names of the variables in the tables
dict=c("mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
       "sdeduscore"="Education (sd)","eduscore2"="Education2 (sd)", "gov_employed"="Gov. Employment (%)", "logprotest"="Protest (log)", 
       "highprofile"="High Profile", "logregistered"="Registered (log)", "female_pct"="Female (%)", "round2"="Runoff", 
       "votebuyingd"="Vote-buying", "fmean.female_pct"="Female", "fmean.logregistered"="Registered (log)", 
       "fmean.pct_urban"="Urban (%)", "fmean.pct_emp"="Employment (%)", "fmean.sdeduscore"="Education (sd)", 
       "fmean.gov_employed"="Gov. Employment (%)", "newndp"="New NDP", "split_ndp"="NDP Dissidents", 
       "incumbent"="Incumbents", "nocand_competing"="Candidates No.", "death_ndpaffil"="NDP Death", "client_index"="Clientelism", 
       "watani_1984" = "NDP 1984 (%)", "wafd_1984" = "MB Coalition 1984 (%)", "fmean.incumbent"="Incumbents", "fmean.nocand_competing"="Candidates No.", 
       "fmean.newndp"="New NDP", "fmean.split_ndp"="NDP Dissidents", "fmean.logprotest"="Protest (log)")

#Define two additional variables for this analysis: log of protests by Muslim Brotherhood, and by non Muslim Brotherhood affiliated actors

data$logmbprotest <- log(data$mbprotest_2000_2005+1)
data$lognonmbprotest <- log(data$protest_2000_2005-data$mbprotest_2000_2005+1)


##### Table (6)

main1 <- feols(mb_dummy ~ pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

main2 <- feols(mb_dummy ~ newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

main3 <- feols(mb_dummy ~  pct_urban + pct_emp + sdeduscore  + lognonmbprotest +logmbprotest + logregistered+female_pct
               +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

main4 <- feols(mb_dummy ~ newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + lognonmbprotest +logmbprotest + logregistered+female_pct
               + incumbent + nocand_competing +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, se.below = T, 
       signif.code = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")



###### Table (7)

main1 <- fenegbin(allintimid ~ newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest +wafd_1984+watani_1984+ logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(allintimid ~ newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest +wafd_1984+watani_1984+ logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- fenegbin(allintimid ~ newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest +wafd_1984+watani_1984+ logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2))     

main4 <- fenegbin(allintimid ~ newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest +wafd_1984+watani_1984+ logregistered+female_pct
                  + incumbent + nocand_competing + round2+round1 | governorate, data3)            


etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, se.below = T, 
       signif.code = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


########## Figure (1)

# Panel (a): Violence against voters

main1 <- fenegbin(voter_total ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(voter_total ~ newndp +  split_ndp +mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- fenegbin(voter_total ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest +wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2)) 

main4 <- fenegbin(voter_total ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing + round2+round1 | governorate, data3)          


fixest::coefplot(list(main1,main2, main3, main4), 
                 keep=c("MB Running", "New NDP", "NDP Dissidents", "Urban", "Employment", "Education", "Protest", "MB Coalition 1984", "NDP 1984"), 
                 dict=c("reset", "mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
                        "sdeduscore"="Education (sd)","newndp"="New NDP", "split_ndp"="NDP Dissidents", "logprotest"="Protest", 
                        "wafd_1984" = "MB Coalition 1984", "watani_1984"="NDP 1984"), 
                 lab.fit="multi", horiz=T, main="") 

legend("bottomright", col = 1:4, lwd = 1, pch=c(16,17,15,13),
       legend = c("2 Rounds", "First", "Second", "3 Rounds"), title = "Model")


# Panel (b): Violence by state actors

main1 <- fenegbin(state_intimidation ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(state_intimidation ~ newndp +  split_ndp +mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- fenegbin(state_intimidation ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest +wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2)) 

main4 <- fenegbin(state_intimidation ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing + round2+round1 | governorate, data3)          


fixest::coefplot(list(main1,main2, main3, main4), 
                 keep=c("MB Running", "New NDP", "NDP Dissidents", "Urban", "Employment", "Education", "Protest", "MB Coalition 1984", "NDP 1984"), 
                 dict=c("reset", "mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
                        "sdeduscore"="Education (sd)","newndp"="New NDP", "split_ndp"="NDP Dissidents", "logprotest"="Protest", 
                        "wafd_1984" = "MB Coalition 1984", "watani_1984"="NDP 1984"), 
                 lab.fit="multi", horiz=T, main="") 

legend("bottomright", col = 1:4, lwd = 1, pch=c(16,17,15,13),
       legend = c("2 Rounds", "First", "Second", "3 Rounds"), title = "Model")



# Panel (c): Violence by non-state actors

main1 <- fenegbin(nonstate_intimidation ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(nonstate_intimidation ~ newndp +  split_ndp +mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- fenegbin(nonstate_intimidation ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest +wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2)) 

main4 <- fenegbin(nonstate_intimidation ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest+wafd_1984+watani_1984 + logregistered+female_pct
                  + incumbent + nocand_competing + round2+round1 | governorate, data3)          


fixest::coefplot(list(main1,main2, main3, main4), 
                 keep=c("MB Running", "New NDP", "NDP Dissidents", "Urban", "Employment", "Education", "Protest", "MB Coalition 1984", "NDP 1984"), 
                 dict=c("reset", "mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
                        "sdeduscore"="Education (sd)","newndp"="New NDP", "split_ndp"="NDP Dissidents", "logprotest"="Protest", 
                        "wafd_1984" = "MB Coalition 1984", "watani_1984"="NDP 1984"), 
                 lab.fit="multi", horiz=T, main="") 

legend("bottomright", col = 1:4, lwd = 1, pch=c(16,17,15,13),
       legend = c("2 Rounds", "First", "Second", "3 Rounds"), title = "Model")


####### Table (8)

main1 <- feols(split_ndp ~ pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

main2 <- feols(split_ndp ~ newndp + mb_dummy +pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

main3 <- feols(newndp ~ pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

main4 <- feols(newndp ~ split_ndp+ mb_dummy + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing +watani_1984+wafd_1984 | governorate, subset(data, data$round==1))

etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, se.below = T, 
       signif.code = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


###### Table (9)

main1 <- fenegbin(allintimid ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$upper==0))

main2 <- fenegbin(allintimid ~newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1 & data$upper==0))            

main3 <- fenegbin(allintimid ~ newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2 & data$upper==0))     

main4 <- fenegbin(allintimid ~ newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing+ round2+round1 | governorate, subset(data3, data3$upper==0))            


etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")

